package simonby.top.annualring.infrastructure.config;

import simonby.top.annualring.domain.model.User;
import simonby.top.annualring.domain.model.FamilyTree;
import simonby.top.annualring.domain.repository.UserRepository;
import simonby.top.annualring.domain.repository.FamilyTreeRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * 数据验证器
 * 在应用启动时验证数据是否正确初始化
 */
@Component
public class DataValidator implements CommandLineRunner {
    
    private final UserRepository userRepository;
    private final FamilyTreeRepository familyTreeRepository;
    
    public DataValidator(
            UserRepository userRepository,
            FamilyTreeRepository familyTreeRepository) {
        this.userRepository = userRepository;
        this.familyTreeRepository = familyTreeRepository;
    }
    
    @Override
    public void run(String... args) throws Exception {
        System.out.println("开始数据验证...");
        
        // 验证管理员用户是否存在
        Optional<User> adminUsers = userRepository.findByUsername("admin");
        if (adminUsers.isEmpty()) {
            System.out.println("警告: 未找到管理员用户");
        } else {
            System.out.println("管理员用户已存在");
        }
        
        // 验证家族树是否存在
        List<FamilyTree> allFamilyTrees = familyTreeRepository.findAll();
        if (allFamilyTrees.isEmpty()) {
            System.out.println("警告: 未找到家族树");
        } else {
            System.out.println("家族树已存在，数量: " + allFamilyTrees.size());
        }
        
        System.out.println("数据验证完成");
    }
}